//
//  WBMainViewController.swift
//  weibo
//
//  Created by z先生 on 17/1/5.
//  Copyright © 2017年 SZT. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
    
        setupComposeButton()
        
        
    }

    //设置支持的方向之后 当前的控制器及子控制器都会遵守这个方向
    //播放视频都是通过Modal展现
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        
        return .portrait
        
    }
        //私有控件
    lazy var composeButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    
   @objc  func composeStatus(){
        
        print("撰写微博")
    
    //测试方向旋转
    
    let vc = UIViewController()
    
//    vc.view.backgroundColor = UIColor.cz_random()
    
    let nav = UINavigationController(rootViewController: vc)
    
    present(nav, animated: true, completion: nil)
    
    }
}

extension WBMainViewController{
    
     func setupComposeButton(){
        
        tabBar.addSubview(composeButton)
        
        let count:CGFloat = CGFloat(childViewControllers.count)
        
        let w = tabBar.bounds.width/count - 1
        
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        print("撰写按钮宽度\(composeButton.bounds.width)")
        
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
//        composeButton.addTarget(self, action: #selector(<#T##@objc method#>), for: <#T##UIControlEvents#>)
    }
    //设置所有子控制器
     func setupChildControllers(){
        
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
        
        var data = NSData(contentsOfFile: jsonPath)
        
        //判断data是否有内容
        if data == nil {
            
            let path = Bundle.main.path(forResource: "main.json", ofType: nil)
            
            data = NSData(contentsOfFile: path!)
            
        }
        
       
        
        
        
        guard let array = try? JSONSerialization.jsonObject(with: data! as Data , options: []) as? [[String:AnyObject]] else{
            
            return
        }
        
        
        
//        let array: [[String: AnyObject]] = [
//        ["clsName":"WBHomeViewController" as AnyObject,"title":"首页" as AnyObject,"imageName":"home" as AnyObject,"visitorInfo": ["imageName":"","message":"关注一些人，回这里看看有什么惊喜"] as AnyObject],
//        ["clsName":"WBMessageViewController" as AnyObject,"title":"消息" as AnyObject,"imageName":"message_center" as AnyObject,"visitorInfo": ["imageName":"visitordiscover_image_message","message":"登录后，别人评论你的微博，发给你的消息，都会在这里收到通知"] as AnyObject],
//        ["clsName":"UIViewController" as AnyObject],
//        ["clsName":"WBDiscoverViewController" as AnyObject,"title":"发现" as AnyObject,"imageName":"discover" as AnyObject,"visitorInfo": ["imageName":"visitordiscover_image_message","message":"登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"] as AnyObject],
//        ["clsName":"WBProfileViewController" as AnyObject,"title":"我" as AnyObject,"imageName":"profile" as AnyObject,"visitorInfo": ["imageName":"visitordiscover_image_profile","message":"登录后，你的微博、相册、个人资料会显示在这里，展示给别人"] as AnyObject]
//        ]
//        
//     let data = try! JSONSerialization.data(withJSONObject: array, options: [])
//        
//       (data as NSData).write(toFile: "/Users/zzz/Desktop/demo.json", atomically: true)
//        
        
        
//        (array as NSArray).write(toFile: "/Users/zzz/Desktop/demo.plist", atomically: true)
        
        var arrayM = [UIViewController]()
        
        for dict in array! {
            
            arrayM.append(controller(dict: dict))
            
        }
        
        viewControllers = arrayM
        
    }
    
    //使用字典创建一个子控制器
   private func controller(dict: [String: AnyObject]) -> UIViewController {
    
        
    guard let clsName = dict["clsName"] as? String,let title = dict["title"] as? String,let imageName = dict["imageName"] as? String,let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? WBBaseViewController.Type,let visitorInfo = dict["visitorInfo"] as? [String:String]
        
            else{
            
            return UIViewController()
            
        }
        
        let vc = cls.init()
        
        vc.title = title
    
        vc.visitorInfo = visitorInfo
    
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
    
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
    
      vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
    
    //设置字体大小
//    vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 15)], for: UIControlState(rawValue: 0))
    
    
    
        let nav = WBMainNavigationController(rootViewController: vc)
        
        return nav
        
    }
}
